/* See LICENSE for copyright and terms of use */

import org.as2lib.test.unit.TestCase;

import org.evtmng.*;
import org.evtmng.events.*;
import org.evtmng.timeline.*;
import org.evtmng.tween.*;

/**
 * @author Tay Ray Chuan
 */

class org.evtmng.test.unit.TimelineManagerTest
	extends TestCase
	implements IStartListener, IRunListener, IEndListener {
	private var m_tlm:TimelineManager;
	private var m_stopTime:Number;

	private static var STARTS:Number;
	private static var RUNS:Number;
	private static var ENDS:Number;

	private var m_starts:Number;
	private var m_runs:Number;
	private var m_ends:Number;

	private var m_id:Number;

	public function setUp():Void {

		var f1:Tween = new Tween(10, 20);
		var f2:Tween = new Tween(10, 15);
		var f3:Tween = new Tween(15, 30);
		var f4:Tween = new Tween(5, 30);
		var tweens:Array = [f1, f2, f3, f4];

		var tl:Timeline = new Timeline();
		var tlm:TimelineManager = new TimelineManager(tl);

		var bcaster:TweenListenerBroadcaster = TweenListenerBroadcaster.instance();
		for(var i:String in tweens) {
			bcaster.addListenerForTween(this, tweens[i]);
		}

		tl.addTween(f1);
		tl.addTween(f2);
		tl.addTween(f3);
		tl.addTween(f4);

		m_stopTime = tl.lastEnd();

		tlm.setTimeInterval(10);

		m_tlm = tlm;

		m_starts = m_ends = m_runs = 0;

		STARTS = 4;
		ENDS = 4;
		RUNS =
		f1.duration() +
		f2.duration() +
		f3.duration() +
		f4.duration();
	}

	public function testTimelineRun():Void {
		m_tlm.runFromTime(0);
		m_id = setInterval(this, "resumeTimelineRun", m_tlm.timeInterval()*m_stopTime*2.5);

		pause();
	}

	public function resumeTimelineRun():Void {
		resume();
		clearInterval(m_id);

		assertEqualsWithMessage("starts: ", m_starts, STARTS);
		assertEqualsWithMessage("runs: ", m_runs, RUNS);
		assertEqualsWithMessage("ends: ", m_ends, ENDS);
	}

	public function onTweenStart(e:TweenEventInfo):Void {
		m_starts++;
	}

	public function whileRunningTween(e:TweenEventInfo):Void {
		m_runs++;
	}

	public function onTweenEnd(e:TweenEventInfo):Void {
		m_ends++;
	}
}
